/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Usuario;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }

    public Usuario buscarUsuario(String nick, String password) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        Usuario usuario = null;

        sql.append("SELECT * ");
        sql.append("FROM usuario u ");
        sql.append("WHERE u.nick = ? ");
        sql.append("AND u.password = ?");

        try {
            query = em.createNativeQuery(sql.toString(), Usuario.class);
            query.setParameter(1, nick);
            query.setParameter(2, password);

            usuario = (Usuario) query.getSingleResult();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_NoResultException:".concat(e.getMessage()));
        } catch (NonUniqueResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_NonUniqueResultException:".concat(e.getMessage()));
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_IllegalStateException:".concat(e.getMessage()));
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_QueryTimeoutException:".concat(e.getMessage()));
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_TransactionRequiredException:".concat(e.getMessage()));
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_PersistenceException:".concat(e.getMessage()));
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuario_Exception:".concat(e.getMessage()));
        } finally {
            return usuario;
        }
    }

    public Usuario buscarUsuarioPorNick(String nick) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        Usuario usuario = null;

        sql.append("SELECT * ");
        sql.append("FROM usuario u ");
        sql.append("WHERE u.nick = ? ");

        try {
            query = em.createNativeQuery(sql.toString(), Usuario.class);
            query.setParameter(1, nick);

            usuario = (Usuario) query.getSingleResult();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_NoResultException:".concat(e.getMessage()));
        } catch (NonUniqueResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_NonUniqueResultException:".concat(e.getMessage()));
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_IllegalStateException:".concat(e.getMessage()));
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_QueryTimeoutException:".concat(e.getMessage()));
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_TransactionRequiredException:".concat(e.getMessage()));
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_PersistenceException:".concat(e.getMessage()));
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarUsuarioPorNick_Exception:".concat(e.getMessage()));
        } finally {
            return usuario;
        }
    }

    public List<Usuario> buscarTodos() {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Usuario> litaUsuarios = null;

        sql.append("SELECT * ");
        sql.append("FROM usuario");

        try {
            query = em.createNativeQuery(sql.toString(), Usuario.class);

            litaUsuarios = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_NoResultException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_IllegalStateException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_QueryTimeoutException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_TransactionRequiredException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_PessimisticLockException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_LockTimeoutException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_PersistenceException:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_buscarTodos_Exception:".concat(e.getMessage()));
            litaUsuarios = new ArrayList<Usuario>();
        } finally {
            return litaUsuarios;
        }
    }
}
